// page/mall/comfirm_product.js
Page({
    data: {
        isHide: 0,
        imgUrls: [
            "/images/index4.jpg",
            "/images/index4.jpg",
            "/images/index4.jpg"
        ],
        indicatorDots: true,
        indicatorColor: "rgba(255,255,255,.3)",
        indicatorActiveColor: "rgba(255,255,255,1)",
        autoplay: true,
        interval: 5000,
        duration: 1000,
        course: {},
        is_score: 0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {


      var that = this
      that.setData({
        options: options,
        isHide: 1,
      })
    },
    onShow: function () {
      var app = getApp()
      var that = this;
      var uid = wx.getStorageSync('uid');
      console.log(uid)
      wx.request({
        url: app.globalData.host + '&g=App&m=Goods&a=order',
        data: { id: that.data.options.id, uid: uid },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res.data)
          that.setData({
            ad: res.data.ad,

          })
          //   console.log(that.data.imgUrls)
        }
      })
      that.setData({
        isHide: 1,
      })
    },
    checkboxChange: function (e) {
      var checked = e.detail.value;
      var length = checked.length;
      if (length == 0) {
        this.data.is_score = 0
      } else {
        this.data.is_score = 1
      }
      console.log(this.data.is_score)
      this.setData({
        is_score: this.data.is_score
      })
    },
    neworder: function () {
      const that = this;
      var app = getApp()
      var uid = wx.getStorageSync('uid');
      var amount
      console.log(that.data)
      console.log(uid)
      // return false
      if (that.data.is_score){
        amount = parseFloat(that.data.ad.price) - parseFloat(that.data.ad.cut)
      }else{
        amount = that.data.ad.price
      }
      wx.request({
        url: app.globalData.host + '&g=App&m=Order&a=neworder', //仅为示例，并非真实的接口地址
        data: {
          uid: uid,
          id: that.data.options.id,
          type: 0,
          is_score: that.data.is_score,
          amount: amount
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
           console.log(res.data)
          if (res.data.status == 1) {
            wx.request({
              url: app.globalData.host + '&g=App&m=Order&a=wx_p1', //仅为示例，并非真实的接口地址
              data: { oid: res.data.oid },
              header: {
                'content-type': 'application/json'
              },
              success: function (res) {
                //console.log(res)
                if (res.data.status == 1) {
                  wx.showToast({
                    title: '支付成功',
                    icon: 'loading',
                    duration: 2000,
                    complete: function (res1) {
                      //page/mall/comfirm_course
                      wx.navigateTo({
                        url: '/page/member/indent'
                      })
                    }
                  })
                  return false;
                }

                wx.requestPayment({
                  'appId': res.data.appId,
                  'timeStamp': res.data.timeStamp,
                  'nonceStr': res.data.nonceStr,
                  'package': res.data.package,
                  'signType': 'MD5',
                  'paySign': res.data.paySign,
                  'success': function (res) {
                    console.log('success');
                    wx.showToast({
                      title: '支付成功',
                      icon: 'loading',
                      duration: 2000,
                      complete: function (res1) {
                        //page/mall/comfirm_course
                        wx.navigateTo({
                          url: '/page/member/indent'
                        })
                      }
                    })
                    // wx.navigateTo({
                    //   url: ''
                    // })
                  },
                  'fail': function (res) {
                    console.log(res);
                    console.log('fail');
                  },
                  'complete': function (res) {
                    console.log(res); console.log('complete');
                  }
                })
              }
            })

          } else {
            wx.showToast({
              title: res.data.info,
              icon: 'cancle',
              duration: 2000
            })
            return false
          }
        }
      });
    },
    
})